var WastageWorkshopItem = function () {
  var template = /* html */ `
    <div class="wastage-workshop-item" :class="{'has-wastage':wastage!==null}">
      <template v-if="wastage!==null">
      <div class="wastage-workshop-item-content">
        <div class="wastage-workshop-item-baseinfo">
          <table cellspacing="0" cellpadding="0">
            <colgroup>
              <col width="27%">
              <col width="23%">
              <col width="27%">
              <col width="23%">
            </colgroup>
            <tr>
              <td>部门</td>
              <td>{{wastage.deptName}}</td>
              <td>车间</td>
              <td>{{wastage.workShopName}}</td>
            </tr>
            <tr>
              <td>当日用电量(kw·h)</td>
              <td>{{formatNumber(wastage.dayElectricUse/1000,2)}}</td>
              <td>当月用电量(kw·h)</td>
              <td>{{formatNumber(wastage.monthElectricUse/1000,2)}}</td>
            </tr>
            <tr>
              <td>当日用气量(升)</td>
              <td>{{formatNumber(wastage.dayGasUse,2)}}</td>
              <td>当月用气量(升)</td>
              <td>{{formatNumber(wastage.monthGasUse,2)}}</td>
            </tr>
            <tr>
              <td>当日用水量(升)</td>
              <td>{{formatNumber(wastage.dayWaterUse,2)}}</td>
              <td>当月用水量(升)</td>
              <td>{{formatNumber(wastage.monthWaterUse,2)}}</td>
            </tr>
            <tr>
              <td>日产能</td>
              <td>{{formatNumber(wastage.dayCapacity,2)}}</td>
              <td>月产能</td>
              <td>{{formatNumber(wastage.monthCapacity,2)}}</td>
            </tr>
          </table>
        </div>
        <div class="wastage-workshop-item-chart" ref="chart"></div>
      </div>
      </template>
    </div>
  `;

  return {
    props: {
      wastage: {
        type: Object,
        required: true
      },
      isShow: {
        type: Boolean,
        required: true
      },
      dateIndex: {
        type: Number,
        required: true
      }
    },

    watch: {
      changeData: function () {
        this.resetChart();
      }
    },

    computed: {
      maxValue: function () {
        var maxValue = this.wastage.maxDataValue;
        if (maxValue === 0) {
          maxValue = 20;
        } else {
          maxValue = Math.max(maxValue * 1.2, 20);
          maxValue = _.ceil(maxValue, -1);
        }
        return maxValue;
      },
      changeData: function () {
        return {
          dateIndex: this.dateIndex,
          isShow: this.isShow
        };
      }
    },

    data: function () {
      return {};
    },

    mounted: function () {
      if (this.isShow) {
        this.showChart();
      }
      var self = this;
      $(this.$el).resize(function () {
        var chartDom = self.$refs['chart'];
        if (chartDom === undefined) {
          return;
        }
        var chart = echarts.getInstanceByDom(chartDom);
        if (chart !== undefined) {
          chart.resize();
        }
      });
    },

    methods: {
      getChart: function () {
        var chartDom = this.$refs['chart'];
        var chart = echarts.getInstanceByDom(chartDom);
        if (chart === undefined) {
          chart = echarts.init(chartDom);
        }
        return chart;
      },

      showChart: function () {
        chart = this.getChart();
        var chartOption = this.getOption();
        this.resetChartOption(chartOption);
        chart.setOption(chartOption, true);
      },

      resetChart() {
        if (!this.isShow) {
          return;
        }

        var chartDom = this.$refs['chart'];
        var chart = echarts.getInstanceByDom(chartDom);
        if (chart === undefined) {
          this.showChart();
          return;
        }

        var chartOption = {};
        this.resetChartOption(chartOption);
        chart.setOption(chartOption);
      },

      getOption: function () {
        var chartOption = {
          color: window.boardChartColor,
          grid: {
            right: 20,
            left: 50,
            bottom: 30,
            top: 30
          },
          xAxis: [{
            type: 'category',
            boundaryGap: false,
            axisLine: {
              lineStyle: {
                color: '#fff'
              }
            },
            axisLabel: {
              color: '#fff',
              fontSize: 12,
              interval: 0,
              formatter: function (value) {
                return moment(value).format('MM-DD');
              }
            }
          }],
          yAxis: [{
            min: 0,
            max: 5,
            name: '单付用电量(w·h)',
            nameGap: 8,
            axisLine: {
              lineStyle: {
                color: '#fff'
              }
            },
            axisLabel: {
              color: '#fff',
              fontSize: 12
            }
          }],
          dataset: {
            source: []
          },
          tooltip: {
            trigger: 'axis',
            formatter: function (params) {
              var param = params[0];
              var data = param.data;
              var texts = [
                param.name,
                `${param.marker}单付用电量:${data.perElectricUse}w·h`
              ];
              return texts.join('<br>');
            }
          },
          series: [{
            type: 'line',
            encode: {
              x: 'createTime',
              y: 'perElectricUse'
            }
          }]
        };

        return chartOption;
      },

      resetChartOption: function (chartOption) {
        _.set(chartOption, 'yAxis[0].max', this.maxValue);

        var chartData = this.wastage.getRecords(this.dateIndex);
        _.set(chartOption, 'dataset.source', chartData);
      },

      formatNumber: function (value, decimal) {
        return SysUtil.decimalNumber(value, decimal);
      }
    },

    template: template
  };
}();
